﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using DevExpress.DashboardCommon;
using DevExpress.DXperience.Demos;

namespace View.Control.Modules
{
    public partial class DashboardTutorialControl : TutorialControlBase {
        public class DataSourceInfo {
            readonly string nam;
            readonly object dat;

            public string Name { get { return nam; } }
            public object Data { get { return dat; } }

            public DataSourceInfo(string nam, object dat) {
                this.nam = nam;
                this.dat = dat;
            }
        }

        static Dashboard CreateCopy(Dashboard dashboard) {
            using(Stream stream = new MemoryStream()) {
                dashboard.SaveToXml(stream);
                stream.Seek(0L, SeekOrigin.Begin);
                Dashboard copy = new Dashboard();
                copy.LoadFromXml(stream);
                for(int i = 0; i < dashboard.DataSources.Count; i++)
                    copy.DataSources[i].Data = dashboard.DataSources[i].Data;
                return copy;
            }
        }
        
        protected Dashboard Dashboard { 
            get { return dashboardViewer.Dashboard; } 
            set {
                if(value != Dashboard) {
                    DisposeDashboard();
                    dashboardViewer.Dashboard = value;
                }
            } 
        }
        protected virtual bool ShowFooterPanel { get { return false; } }

        public DashboardTutorialControl() {
            InitializeComponent();
            panelFooter.Visible = ShowFooterPanel;
            LoadDashboard();
        }
        void LoadDashboard() {
            Assembly asm = Assembly.GetExecutingAssembly();
            string fileName = string.Format(@"{0}.xml", GetType().Name);
            Stream stream = asm.GetManifestResourceStream(@"View.Dashboards." + fileName); //new FileStream(fileName, FileMode.Open, FileAccess.Read);// asm.GetManifestResourceStream(@"View.Dashboards." + fileName);
            if(stream == null)
                stream = asm.GetManifestResourceStream(fileName);
            if(stream != null) {
                Dashboard dashboard = new Dashboard();
                dashboard.LoadFromXml(stream);
                IList<DataSourceInfo> data = LoadData();
                if(data != null)
                    foreach(DataSourceInfo info in data) {
                        string name = info.Name;
                        DataSource datSource = dashboard.DataSources.FindFirst(ds => ds.Name == name);
                        if(datSource != null)
                            datSource.Data = info.Data;
                        else
                            dashboard.DataSources.Add(new DataSource(info.Name, info.Data));

                    }
                Dashboard = dashboard;
                stream.Dispose();
            }
            else
                Dashboard = null;
        }
        void DisposeDashboard() {
            Dashboard dashboard = Dashboard;
            if(dashboard != null)
                dashboard.Dispose();
        }
        void btnEditDashboard_Click(object sender, EventArgs e) {
            //using(DashboardDesignerForm designerForm = new DashboardDesignerForm(CreateCopy(Dashboard))) {
            //    designerForm.ShowDialog();
            //    if(designerForm.SaveDashboard)
            //        Dashboard = CreateCopy(designerForm.Dashboard);
            //}
        }
        protected virtual IList<DataSourceInfo> LoadData() {
            return null;
        }
    }
}
